<?php
/**
* Copyright (c) 2002-2007 BioWare Corp.
* http://www.bigdaddymonitor.org
*
* This file is part of the BigDaddy Monitoring
* program. It is free software distributed under
* the terms of the BSD License. It is ditributed
* in the hopes that it will be useful, but
* WITHOUT ANY WARRANTY; without event the implied
* warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE.
*
* The BSD License is available from:
* http://www.opensource.org/licenses/bsd-license.php
**/
require_once(CLASS_ROLE_ADMIN);

$RoleAdmin = new RoleAdmin;


if (!$BigDaddy->IsUserAllowedByAction("manage-role-system"))
{
    header ("Location: /");
    exit;
}

$displayParams["applicationCSS"]="app_roleadmin.css";
$displayParams["sortableTables"] = true;

// set up links/sidebar/etc for the new theme.
//include($_SERVER["DOCUMENT_ROOT"]."/tabs.inc");

//include("newchrome.inc");
//include("../newtheme.inc");
include("newtheme.inc");

function handleResponse($aErrors, $aMessages, $sRedirect = '/admin/role/')
{
    if(count($aErrors) > 0)
    {
	$sError = join("<br/>", $aErrors);
	$_SESSION['ming_error'] = $sError;
    }
    if(count($aMessages) > 0)
    {
	$sMessage = join("<br/>", $aMessages);
	$_SESSION['ming_message'] = $sMessage;
    }

    header("Location: " . $sRedirect);
    exit;
}
    
// grab many, many query variables.
$sPage = $_REQUEST['page'];
$bSubmit = (strlen($_REQUEST['submit']) > 0);

$sName = $_REQUEST['name'];
$sCode = $_REQUEST['code'];
$nID = (int)$_REQUEST['ID'];
$sStringID = $_REQUEST['stringID'];
$sRoleStringID = $_REQUEST['roleStringID'];
$nDefID = (int)$_REQUEST['nDefID'];
$sType = $_REQUEST['type'];
$nTypeID = (int)$_REQUEST["nTypeID"];
$nRoleTypeID = (int)$_REQUEST['roleTypeID'];
$nMaxAllowed = (int)$_REQUEST['maxAllowed'];
$sNotes = $_REQUEST['notes'];
$nRoleID = (int)$_REQUEST['roleID'];

$sGroupID = $_REQUEST['groupID'];
$sSelectGroupID = $_REQUEST['selectGroupID'];
$nActionID = (int)$_REQUEST['actionID'];

$sDescription = $_REQUEST['description'];
$sOverrideDesc = $_REQUEST['overridedesc'];

$nRoleDefinitionID = (int)$_REQUEST["roleDefinitionID"];
$sRoleDefinitionString = $_REQUEST["roleDefinitionString"];
$sRoleDefinitionDescription = $_REQUEST["roleDefinitionDescription"];

$sPrimaryRoleID = $_REQUEST["primaryRoleID"];
$sSecondaryRoleID = $_REQUEST["secondaryRoleID"];
$sRelationshipType = $_REQUEST["relationshipType"];
$nRoleTypeRelID = $_REQUEST['nRoleTypeRelID'];

$nRoleRelID = $_REQUEST['nRoleRelID'];
$aActionIDs = $_REQUEST['actionID'];

$nGlobalActionID = (int)$_REQUEST['nGlobalActionID'];
$sUsername = $_REQUEST["username"];

$sPrimaryUsername = $_REQUEST["primaryUsername"];
$sSecondaryUsername = $_REQUEST["secondaryUsername"];
$nUserRelID = (int)$_REQUEST['nUserRelID'];
$sBatchRoleDefs = $_REQUEST["batchroledefinitions"];

switch ($sPage)
{
    case "setroletype":
	if ($bSubmit)
	{
	    $aInfo["name"] = $sName;
	    $aInfo["maxAllowed"] = $nMaxAllowed;
	    $aInfo["description"] = $sDescription;
	    $aInfo["stringID"] = $sStringID;
	    
	    //Is this an edit?
	    if ($nTypeID > 0)
	    {
		$nReturn = $RoleAdmin->SetRoleType($aInfo, $nTypeID);
	    }
	    else
	    {
		$nReturn = $RoleAdmin->SetRoleType($aInfo);
	    }
	    
	    if ($nReturn > 0)
	    {
		header("Location: ".$_SERVER["PHP_SELF"]."?page=listroletypes");
		exit;
	    }
	    else
	    {
		handleResponse($RoleAdmin->aErrors, $RoleAdmin->aMessages);
	    }
	}
	break;
    case "addroletype":
	$pageParams["include"]="form-roletype.inc";
	$pageParams["title"]="Add Role Type";
	break;
    case "editroletype":
	$aInfo=$RoleAdmin->GetRoleTypeByID($nID);
	$pageParams["include"]="form-roletype.inc";
	$pageParams["title"]="Edit Role Type: " . $aInfo["name"];
	break;
    case "listroletypes":
	$pageParams["include"]="listroletypes.inc";
	$pageParams["title"]="List All Role Types";
	break;
    case "removeroletype":
	$RoleAdmin->RemoveRoleTypeByID($nID);
	header("Location: ".$_SERVER["PHP_SELF"]."?page=listroletypes");
	break;
    case "setroledef":
	if ($bSubmit)
	{
	    $aInfo["name"] = $sName;
	    $aInfo["code"]=$sCode;
	    if ($sOverrideDesc != "")
	    {
		$aInfo["description"] = $sOverrideDesc;
	    }
	    else
	    {
		$aInfo["description"] = $sDescription;
	    }
	    
	    //Is this an edit?
	    if ($nDefID > 0)
	    {
		$nReturn=$RoleAdmin->SetRoleDefinition($aInfo, $nDefID);
	    }
	    else
	    {
		$nReturn=$RoleAdmin->SetRoleDefinition($aInfo);
	    }
	    
	    if ($nReturn > 0)
	    {
		header("Location: {$_SERVER["PHP_SELF"]}?page=listroledef&type={$aInfo["description"]}");
		exit;
	    }
	    else
	    {
		handleResponse($RoleAdmin->aErrors, $RoleAdmin->aMessages);
	    }
	}
	break;
    case "addroledef":
	$pageParams["include"]="form-roledef.inc";
	$pageParams["title"]="Add Role Definition";
	break;
    case "editroledef":
	$aInfo=$RoleAdmin->GetRoleDefinitionByID($nID);
	$pageParams["include"]="form-roledef.inc";
	$pageParams["title"]="Edit Role Definition: " . $aInfo["name"];
	break;
    case "listroledef":
	$pageParams["include"]="listroledef.inc";
	$pageParams["title"]="List All Role Definitions: $sType";
	break;
    case "removeroledef":
	$RoleAdmin->RemoveRoleDefinitionByID($nID);
	header("Location: ".$_SERVER["PHP_SELF"]."?page=listroledef&type=$sType");
	break;
    case "setrole":
	if ($bSubmit)
	{
	    //If there is a roleDefinitionString instead of an ID, we want to add that in rather 
	    // than choose...
            if ($sRoleDefinitionString != "")
	    {
		$aInfo["name"] = $sRoleDefinitionString;
		$aInfo["description"] = $sRoleDefinitionDescription;

		$nRoleDefID = $RoleAdmin->SetRoleDefinition($aInfo);
		unset($aInfo);
	    }
            elseif ($nRoleDefinitionID > 0)
	    {
		$nRoleDefID = $nRoleDefinitionID;
		
	    }
		print_r($aInfo);
	    
	    //Now we have to check whether we have a Role Definition ID if not
            //we cannot enter this role
            if ($nRoleDefID > 0)
	    {
		$aInfo["roleTypeID"] = $nRoleTypeID;
		$aInfo["roleDefinitionID"] = $nRoleDefID;
		$aInfo["roleStringID"] = $sRoleStringID;
		$aInfo["notes"] = $sNotes;
	
		//Is this an edit?
		if ($nRoleID > 0)
		{
		    $nReturn = $RoleAdmin->SetRole($aInfo, $nRoleID);
		}
		else
		{
		    $nReturn = $RoleAdmin->SetRole($aInfo);
		}
	    }
	    
	    if ($nReturn > 0)
	    {
		header("Location: ".$_SERVER["PHP_SELF"]."?page=listrole&type=".$aInfo["roleTypeID"]);
		exit;
	    }
	    else
	    {
		print_r($aInfo);
		exit;
//		handleResponse($RoleAdmin->aErrors, $RoleAdmin->aMessages);
	    }
	}
	break;
    case "editrolebatch":
	    $aInfo=$RoleAdmin->GetRoleDefinitionByID($nID);
	    $pageParams["include"]="form-rolebatch.inc";
	    $pageParams["title"]="Add Roles By Batch: ".$aInfo["name"];
	    break;
    case "setrolebatch":
	if ($bSubmit)
	{
		$sTime = time().md5(LOGGEDIN_USERNAME);
		$fh = fopen("/tmp/setrole".$sTime, "w+");
		fwrite($fh, stripslashes($sBatchRoleDefs));
		fclose($fh);
		
		$fh = fopen("/tmp/setrole".$sTime, "r");

		while (($aCSVInfo = fgetcsv($fh, 1000, ",")) !== FALSE)
		{
			//If there is a roleDefinitionString instead of an ID, we want to add that in rather than
			//choose...

			$aInfo["name"]=trim($aCSVInfo[0]);
			$aInfo["description"]=trim($aCSVInfo[1]);
			$nRoleDefID=$RoleAdmin->SetRoleDefinition($aInfo);
			unset($aInfo);
			
			//Now we have to check whether we have a Role Definition ID if not
			//we cannot enter this role
			if ($nRoleDefID>0)
			{	
				$bAddRole=true;
			}
	
			if ($bAddRole)
			{
				$aInfo["roleTypeID"] = $nRoleTypeID;
				$aInfo["roleDefinitionID"] = $nRoleDefID;
				$aInfo["roleStringID"]=trim($aCSVInfo[2]);
				$aInfo["notes"]=trim($aCSVInfo[3]);

				$nReturn=$RoleAdmin->SetRole($aInfo);
			}

		}
		
		fclose($fh);
		@unlink("/tmp/setrole".$sTime);
	    
	    if ($nReturn>0)
	    {
			header("Location: ".$_SERVER["PHP_SELF"]."?page=listrole&type=".$aInfo["roleTypeID"]);
			exit;
	    }
	    else
	    {
		handleResponse($RoleAdmin->aErrors, $RoleAdmin->aMessages);
	    }
	}
    case "addrole":
	$pageParams["include"]="form-role.inc";
	$pageParams["title"]="Add Role";
	break;
    case "editrole":
	$aInfo = $RoleAdmin->GetRoleByID($nID);
	$pageParams["include"]="form-role.inc";
	$pageParams["title"]="Edit Role: ID - ".$aInfo["ID"];
	break;
    case "listrole":
	$aTypeInfo = $RoleAdmin->GetRoleTypeByID($sType);
	$pageParams["include"]="listrole.inc";
	$pageParams["title"]="List Roles: ".$aTypeInfo["name"];
	break;
    case "removerole":
	$RoleAdmin->RemoveRoleByID($nID);
	header("Location: ".$_SERVER["PHP_SELF"]."?page=listrole&type=".$sType);
	break;
    case "setroleaction":
	if ($bSubmit)
	{
	    $aInfo["name"] = $sName;
	    $aInfo["type"] = $sType;
	    $aInfo["groupID"]=$sGroupID;
		$aInfo["selectGroupID"] = $sSelectGroupID;
		
		if ($aInfo["groupID"] == "" && $aInfo["selectGroupID"] != "")
		{
			$aInfo["groupID"] = $aInfo["selectGroupID"];
		}
    
	    //Is this an edit?
	    if ($nActionID > 0)
	    {
			$nReturn = $RoleAdmin->SetRoleAction($aInfo, $nActionID);
	    }
	    else
	    {
			$nReturn = $RoleAdmin->SetRoleAction($aInfo);
	    }
	    
	    if ($nReturn>0)
	    {
			header("Location: ".$_SERVER["PHP_SELF"]."?page=listroleactions");
			exit;
	    }
	    else
	    {
		handleResponse($RoleAdmin->aErrors, $RoleAdmin->aMessages);
	    }
	}
    case "addroleaction":
	$pageParams["include"]="form-roleaction.inc";
	$pageParams["title"]="Add Role Action";
	break;
    case "editroleaction":
	$aInfo=$RoleAdmin->GetRoleActionByID($nID);
	$pageParams["include"]="form-roleaction.inc";
	$pageParams["title"]="Edit Role Action: ".$aInfo["name"];
	break;
    case "listroleactions":
	$pageParams["include"]="listroleactions.inc";
	$pageParams["title"]="List Roles Actions";
	break;
    case "removeroleaction":
	$RoleAdmin->RemoveRoleActionByID($nID);
	header("Location: ".$_SERVER["PHP_SELF"]."?page=listroleactions");
	break;
    case "setrolerel":
	if ($bSubmit)
	{
	    $aInfo["primaryRoleID"] = $sPrimaryRoleID;
	    $aInfo["secondaryRoleID"] = $sSecondaryRoleID;
	    $aInfo["relationshipType"] = $sRelationshipType;
	    // changed actionID to be a multiple select, and thus an array --jesse@bioware.come
    
	    //Is this an edit?
	    if ($nRoleRelID > 0)
	    {
		if(is_array($aActionIDs))
		{
		    $aInfo["actionID"] = $aActionIDs[0]; // only use the first one, to preserve original behaviour.
		}
		else // must be a scalar.
		{
		    $aInfo["actionID"] = $aActionIDs;
		}
		$nReturn = $RoleAdmin->SetRoleRelationship($aInfo, $nRoleRelID);
	    }
	    else
	    {
		if(is_array($aActionIDs))
		{
		    foreach($aActionIDs as $nActionID) // add them all!
		    {
			$aInfo["actionID"] = $nActionID; // clobber previous value.
			$nReturn = $RoleAdmin->SetRoleRelationship($aInfo);
		    }
		}
		else // must be a scalar.
		{
		    $aInfo["actionID"] = $aActionIDs;
		    $nReturn = $RoleAdmin->SetRoleRelationship($aInfo);
		}
	    }
	    
	    if ($nReturn>0)
	    {
		header("Location: ".$_SERVER["PHP_SELF"]."?page=listrolerels&type=".$aInfo["relationshipType"]);
		exit;
	    }
	    else
	    {
		handleResponse($RoleAdmin->aErrors, $RoleAdmin->aMessages);
	    }
	}
    case "listrolerels":
	$pageParams["include"]="listrolerels.inc";
	$pageParams["title"]="List Roles Relationships";
	break;
    case "addrolerel":
	$pageParams["include"]="form-rolerel.inc";
	$pageParams["title"]="Add Role Relationship";
	break;
    case "editrolerel":
	$aInfo=$RoleAdmin->GetRoleRelationshipByID($nID);
	$pageParams["include"]="form-rolerel.inc";
	$pageParams["title"]="Edit Role Relationship";
	break;
    case "removerolerel":
	$RoleAdmin->RemoveRoleRelationshipByID($nID);
	header("Location: ".$_SERVER["PHP_SELF"]."?page=listrolerels&type=".$sType);
	break;
    case "setroletyperel":
	if ($bSubmit)
	{
	    $aInfo["primaryRoleID"] = $sPrimaryRoleID;
	    $aInfo["roleTypeID"] = $nRoleTypeID;
	    $aInfo["relationshipType"] = $sRelationshipType;
	    // changed actionID to be a multi select, and thus an array. --jesse@bioware.com
    
	    //Is this an edit?
	    if ($nRoleTypeRelID > 0)
	    {
		if(is_array($aActionIDs))
		{
		    $aInfo["actionID"] = $aActionIDs[0];
		}
		else // must be a scalar
		{
		    $aInfo["actionID"] = $aActionIDs;
		} 
		$nReturn = $RoleAdmin->SetRoleTypeRelationship($aInfo, $nRoleTypeRelID);
	    }
	    else
	    {
		if(is_array($aActionIDs))
		{
		    foreach($aActionIDs as $nActionID) // add them all!
		    {
			$aInfo["actionID"] = $nActionID; // clobber previous value
			$nReturn = $RoleAdmin->SetRoleTypeRelationship($aInfo);
		    }
		}
		else // must be a scalar
		{
		    $aInfo["actionID"] = $aActionIDs;
		    $nReturn = $RoleAdmin->SetRoleTypeRelationship($aInfo);
		}
	    }
	    
	    if ($nReturn > 0)
	    {
		header("Location: ".$_SERVER["PHP_SELF"]."?page=listroletyperels&type=".$aInfo["relationshipType"]);
		exit;
	    }
	    else
	    {
		handleResponse($RoleAdmin->aErrors, $RoleAdmin->aMessages);
	    }
	}
    case "listroletyperels":
	$pageParams["include"]="listroletyperels.inc";
	$pageParams["title"]="List Roles Relationships";
	break;
    case "addroletyperel":
	$pageParams["include"]="form-roletyperel.inc";
	$pageParams["title"]="Add RoleType Relationship";
	break;
    case "editroletyperel":
	$aInfo = $RoleAdmin->GetRoleTypeRelationshipByID($nID);
	$pageParams["include"]="form-roletyperel.inc";
	$pageParams["title"]="Edit RoleType Relationship";
	break;
    case "removeroletyperel":
	$RoleAdmin->RemoveRoleTypeRelationshipByID($nID);
	header("Location: ".$_SERVER["PHP_SELF"]."?page=listroletyperels&type=".$sType);
	break;
    case "setglobalaction":
	if ($bSubmit)
	{
	    $aInfo["roleID"] = $nRoleID;
	    $aInfo["actionID"] = $nActionID;
    
	    //Is this an edit?
	    if ($nGlobalActionID > 0)
	    {
		$nReturn = $RoleAdmin->SetRoleGlobalAction($aInfo, $nGlobalActionID);
	    }
	    else
	    {
		$nReturn = $RoleAdmin->SetRoleGlobalAction($aInfo);
	    }
	    
	    if ($nReturn > 0)
	    {
		header("Location: ".$_SERVER["PHP_SELF"]."?page=listglobalactions");
		exit;
	    }
	    else
	    {
		handleResponse($RoleAdmin->aErrors, $RoleAdmin->aMessages);
	    }
	}
    case "listglobalactions":
	$pageParams["include"]="listglobalactions.inc";
	$pageParams["title"]="List Roles Global Actions";
	break;
    case "addglobalaction":
	$pageParams["include"]="form-globalaction.inc";
	$pageParams["title"]="Add Global Action";
	break;
    case "editglobalaction":
	$aInfo=$RoleAdmin->GetRoleGlobalActionByID($nID);
	$pageParams["include"]="form-globalaction.inc";
	$pageParams["title"]="Edit Global Action";
	break;
    case "removeglobalaction":
	$RoleAdmin->RemoveRoleGlobalActionByID($nID);
	header("Location: ".$_SERVER["PHP_SELF"]."?page=listglobalactions");
	break;
    case "setroleuser":
	if ($bSubmit)
	{
	    $aInfo["roleID"] = $nRoleID;
	    $aInfo["username"] = $sUsername;

	    
	    $Role->clearErrors();
	    $nReturn = $Role->SetRoleByUsername($aInfo["roleID"], $aInfo["username"], array("roleGlobal" => "manage-role-system"), "rolesystem");
    
	    if ($nReturn >= 0)
	    {
		header("Location: ".$_SERVER["PHP_SELF"]."?page=listroleusers&ID=".$aInfo["roleID"]);
		exit;
	    }
	    else
	    {
		handleResponse($Role->aErrors, $Role->aMessages);
	    }
	}
	break;
    case "listroleusers":
	$aRoleInfo = $RoleAdmin->GetRoleByID($nID);
	$pageParams["include"]="listroleusers.inc";
	$pageParams["title"]="List Users In Role: ".$aRoleInfo["roleName"];
	break;
    case "removeroleuser":
	$Role->RemoveRoleByUsername($nRoleID, $sUsername, array("roleGlobal" => "manage-role-system"), "rolesystem");
	header("Location: ".$_SERVER["PHP_SELF"]."?page=listroleusers&ID=".$nRoleID);
		exit;
    case "listusers":
	$pageParams["include"]="listusers.inc";
	$pageParams["title"]="List Users";
	break;
    case "setuserrel":
	if ($bSubmit)
	{
	    $aInfo["primaryUsername"] = $sPrimaryUsername;
	    $aInfo["secondaryUsername"] = $sSecondaryUsername;
	    $aInfo["relationshipType"] = $sRelationshipType;
	    // changed actionID to be a multiple select, and thus an array --jesse@bioware.come
	    //Is this an edit?
	    if ($nUserRelID > 0)
	    {
		if(is_array($aActionIDs))
		{
		    $aInfo["actionID"] = $aActionIDs[0]; // only use the first one, to preserve original behaviour.
		}
		else // must be a scalar.
		{
		    $aInfo["actionID"] = $aActionIDs;
		}
		$nReturn = $RoleAdmin->SetUserRelationship($aInfo, $nUserRelID);
	    }
	    else
	    {
		if(is_array($aActionIDs))
		{
		    foreach($aActionIDs as $nActionID) // add them all!
		    {
			$aInfo["actionID"] = $nActionID; // clobber previous value.
			$nReturn = $RoleAdmin->SetUserRelationship($aInfo);
		    }
		}
		else // must be a scalar.
		{
		    $aInfo["actionID"] = $aActionIDs;
		    $nReturn = $RoleAdmin->SetUserRelationship($aInfo);
		}
	    }
	    
	    if ($nReturn > 0)
	    {
		header("Location: ".$_SERVER["PHP_SELF"]."?page=listuserrels&type=".$aInfo["relationshipType"]);
		exit;
	    }
	    else
	    {
		handleResponse($RoleAdmin->aErrors, $RoleAdmin->aMessages);
	    }
	}
    case "listuserrels":
	$pageParams["include"]="listuserrels.inc";
	$pageParams["title"]="List User Relationships";
	break;
    case "adduserrel":
	$pageParams["include"]="form-userrel.inc";
	$pageParams["title"]="Add User Relationship";
	break;
    case "edituserrel":
	$aInfo = $RoleAdmin->GetUserRelationshipByID($nID);
	$pageParams["include"]="form-userrel.inc";
	$pageParams["title"]="Edit User Relationship";
	break;
    case "removeuserrel":
	$RoleAdmin->RemoveUserRelationshipByID($nID);
	header("Location: ".$_SERVER["PHP_SELF"]."?page=listuserrels&type=$sType");
	break;
    case "":
    default:
	$pageParams["include"]="index.inc";
	$pageParams["title"]="Role System Administration";
}


?>
